﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
namespace Project01
{
  public  class ManagerGame
    {
        MonsterComponent monster;
         PlayerComponent player;
      TimeSpan Time;
      public ManagerGame(Game game)
      {
          if (player == null && monster == null)
          {
              game.Components.Add(monster = new MonsterComponent(game));
              game.Components.Add(player = new PlayerComponent(game));
          }
          
      }


      private bool PlayerwithMonster_Collision()
      {
          for (int i = 0; i < monster.MList.Count; i++)
          {
              if (player.Player.Sprite.rectangle(player.Player.Sprite.Position).Intersects(monster.MList[i].Sprite.rectangle(monster.MList[i].Sprite.Position)))
              {
                  if (PixelCollision(player.Player.Sprite.Texture, monster.MList[i].Sprite.Texture,
                         player.Player.Sprite.rectangle(player.Player.Sprite.Position), monster.MList[i].Sprite.rectangle(monster.MList[i].Sprite.Position)))
                  {

                      return true;
                  }

              }

          }
          return false;
      }
      private bool PlayerwithBulletsMonster_Collision()
      {
          for (int i = 0; i < monster.MList.Count; i++)
          {
              for (int j = 0; j < monster.MList[i].Mbullets.Count; j++)
              {
                  if (player.Player.Sprite.rectangle(player.Player.Sprite.Position).Intersects(monster.MList[i].Mbullets[j].Sprite.rectangle(monster.MList[i].Mbullets[j].Sprite.Position)))
                  {
                      if (PixelCollision(player.Player.Sprite.Texture, monster.MList[i].Mbullets[j].Sprite.Texture,
                             player.Player.Sprite.rectangle(player.Player.Sprite.Position), monster.MList[i].Mbullets[j].Sprite.rectangle(monster.MList[i].Mbullets[j].Sprite.Position)))
                      {

                          return true;
                      }
                  }
              }

          }
          return false;
      }
      private bool MonsterwithBulletsPlayer_Collision()
      {
          for (int i = 0; i < player.Player.bullets.Count; i++)
          {
              for (int j = 0; j < monster.MList.Count; j++)
              {
                  if (monster.MList[j].Type != 6)
                  {
                      if (player.Player.bullets[i].Sprite.rectangle(player.Player.bullets[i].Sprite.Position).Intersects(monster.MList[j].Sprite.rectangle(monster.MList[j].Sprite.Position)))
                      {
                          // if (PixelCollision(monster.MList[j].Sprite.Texture, player.Player.bullets[i].Sprite.Texture,
                          //  monster.MList[j].Sprite.rectangle(monster.MList[j].Sprite.Position), player.Player.bullets[i].Sprite.rectangle(player.Player.bullets[i].Sprite.Position)))
                          // {
                          monster.MList[j].SetHealth(player.Player.bullets[i].Damage);
                          monster.MList[j].Sprite.Color = Color.Red;
                          player.Player.bullets[i] = null;
                          player.Player.bullets.RemoveAt(i);

                          return true;
                          //  }
                      }
                      else
                      {
                          monster.MList[j].Sprite.Color = Color.White;
                      }
                  }
              }
          }
          return false;
      }
      private bool PixelCollision(Texture2D sprite1, Texture2D sprite2, Rectangle player, Rectangle enemy)
      {

          Color[] colorData1 = new Color[sprite1.Width * sprite1.Height];

          Color[] colorData2 = new Color[sprite2.Width * sprite2.Height];

          sprite1.GetData<Color>(colorData1);

          sprite2.GetData<Color>(colorData2);

          int top, bottom, left, right;



          top = Math.Max(player.Top, enemy.Top);

          bottom = Math.Min(player.Bottom, enemy.Bottom);

          left = Math.Max(player.Left, enemy.Left);

          right = Math.Min(player.Right, enemy.Right);



          for (int y = top; y < bottom; y++)
          {

              for (int x = left; x < right; x++)
              {

                  Color A = colorData1[(y - player.Top) * (player.Width) + (x - player.Left)];

                  Color B = colorData2[(y - enemy.Top) * (enemy.Width) + (x - enemy.Left)];



                  if ((A.A != 0 && B.A != 0))
                  {
                      //Console.WriteLine("x");
                      return true;
                  }

              }

          }

          return false;

      }




      public void Update(GameTime gameTime)
      {


          //Note:Check Collision follow methods PixelCollision
          //Check Collision between player and monster
          // Check Collision between player and bullets of monster
          if (Time.TotalMilliseconds > 0)
          {
              Time += gameTime.ElapsedGameTime;
              if (Time >= TimeSpan.FromSeconds(3))
                  Time = TimeSpan.Zero;
          }
          if (PlayerwithMonster_Collision() && Time == TimeSpan.Zero
              || PlayerwithBulletsMonster_Collision() && Time == TimeSpan.Zero)
          {

              Time += gameTime.ElapsedGameTime;

          }

          MonsterwithBulletsPlayer_Collision();//check Collision between bullets of player and monster
         
      }
    }
}
